package com.linked.mystyle.leaveword.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linked.mystyle.leaveword.bean.LeaveWordListRequest;
import com.linked.mystyle.leaveword.bean.LeaveWordPO;
import com.linked.mystyle.leaveword.bean.LeaveWordReturnPO;
import com.linked.mystyle.leaveword.bean.ReturnWordRequest;
import com.linked.mystyle.leaveword.mapper.ILeaveWordReturnMapper;
import com.linked.mystyle.leaveword.service.ILeaveWordReturnService;
import com.linked.mystyle.leaveword.service.ILeaveWordService;
import com.linked.universal.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author :dbq
 * @date : 2022/9/30 14:17
 */
@RestController
@RequestMapping("leaveword")
public class LeaveWordController {

    private final static Logger logger = LoggerFactory.getLogger(LeaveWordController.class);
    private final ILeaveWordService leaveWordService;

    private final ILeaveWordReturnService leaveWordReturnService;

    @Autowired
    public LeaveWordController(ILeaveWordService leaveWordService, ILeaveWordReturnService leaveWordReturnService) {
        this.leaveWordService = leaveWordService;
        this.leaveWordReturnService = leaveWordReturnService;
    }

    @PostMapping("addALeaveWord")
    Result addALeaveWord(@RequestBody LeaveWordPO request) {
        Boolean res = false;
        try {
            res = leaveWordService.addALeaveWord(request);
        } catch (Exception e) {
            logger.error("添加留言异常", e);
            return Result.error("添加留言异常");
        }
        return res ? Result.ok(true, "添加留言成功！") : Result.ok(false, "添加留言失败！");
    }

    @PostMapping("queryLeaveWordList")
    Result queryLeaveWordList(@RequestBody LeaveWordListRequest request) {
        IPage<LeaveWordPO> res = null;
        try {
            res = leaveWordService.queryLeaveWordList(request);
        } catch (Exception e) {
            logger.error("查询留言列表异常", e);
            return Result.error();
        }
        return Result.success(res);
    }

    @PostMapping("returnLeaveWord")
    Result returnLeaveWord(@RequestBody LeaveWordReturnPO request) {
        Boolean res = false;
        try {
            res = leaveWordService.returnLeaveWord(request);
        } catch (Exception e) {
            logger.info("回复留言异常", e);
            return Result.error();
        }
        return res ? Result.ok(true, "回复留言成功") : Result.ok(false, "回复留言失败");
    }

    @PostMapping("queryReturnWordList")
    Result queryReturnWordList(@RequestBody ReturnWordRequest request) {

        IPage<LeaveWordReturnPO> res = null;
        try {
            res = leaveWordReturnService.queryReturnWordList(request);
        } catch (Exception e) {
            logger.info("查询回复留言列表异常", e);
            return Result.error();
        }
        return Result.success(res);
    }

    @PostMapping("deleteLeaveWord")
    Result deleteLeaveWord(@RequestBody LeaveWordPO param) {
        if (StringUtils.isEmpty(param.getLeavewordId())) {
            return Result.error("留言id不能为null!");
        }
        boolean ret = false;
        try {
            ret = leaveWordService.deleteLeaveWord(param);
        } catch (Exception e) {
            logger.error("删除留言异常！");
        }
        return ret ? Result.ok("删除留言成功！") : Result.error("删除留言失败！");
    }

    @PostMapping("deleteLeaveWordReturn")
    Result deleteLeaveWordReturn(@RequestBody LeaveWordReturnPO param) {
        if (StringUtils.isEmpty(param.getLeavewordReturnId())) {
            return Result.error("回复留言id不能为null!");
        }
        boolean ret = false;
        try {
            ret = leaveWordReturnService.deleteLeaveWordReturn(param);
        } catch (Exception e) {
            logger.error("删除回复留言异常！");
        }
        return ret ? Result.ok("删除回复留言成功！") : Result.error("删除回复留言失败！");
    }


}
